/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;


import DTO.VeDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Nobita Chau
 */
public class VeDAO {
    
     public static boolean Them(VeDTO n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO `quanlyduongsat`.`ve` ( `tinh_trang_ve`, `ghe`, `gia_ve`, `chuyen`, `hoa_don`, `ngay_tao`, `ga_di`, `ga_den`, `doi_tuong`) "
                    + "VALUES ( '%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%d');",n.TinhTrangVe,n.Ghe,n.GiaVe,n.Chuyen,n.HoaDon,n.NgayTao,n.GaDi,n.GaDen,n.DoiTuong);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           System.out.println(ex);
        }
        return false;
    }

    public static boolean Xoa(int id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from ve where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setInt(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select * from ve";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(int MaTuyen)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From ve Where id like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, MaTuyen);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(VeDTO C, int id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "UPDATE ve SET tinh_trang_ve = ?, ghe = ?,gia_ve = ?,chuyen = ?,hoa_don = ?,ngay_tao = ?,ga_di = ?, ga_den=?, doi_tuong=? WHERE id =?";
                            
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, C.TinhTrangVe);
            ps.setString(2, C.Ghe);
            ps.setInt(3, C.GiaVe);
            ps.setString(4, C.Chuyen);
            ps.setString(5, C.HoaDon);
            ps.setString(6, C.NgayTao);
            ps.setInt(7, C.GaDi);
            ps.setInt(8, C.GaDen);
            ps.setInt(9, C.DoiTuong);
            ps.setInt(10,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
}
